﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Runtime.InteropServices;
using File_SyncUI.Properties;
using hxc;
using hxc.Win32;

namespace File_SyncUI
{
	public partial class FS_MAINDLG : Form
	{
        private Configuration m_Config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
		private int m_FormCloseFlag = 0;
        private CBrowserTB m_ToolBar = null;
        private CBrowserButton m_btnForward = new CBrowserButton();
        private CBrowserButton m_btnBackward = new CBrowserButton();
        private TreeView m_TreeView = null;
        private FileSystemView m_ListView = null;
        public static Color BkgundColor
        {
            get { return Color.FromArgb(240, 240, 240); }
        }

		public FS_MAINDLG()
		{
			InitializeComponent();
            m_ToolBar = new CBrowserTB(this, new Point(1, 45), 40);
            m_TreeView = new TreeView();
            m_ListView = new FileSystemView(this, @"D:\Backup\我的文档\Google 云端硬盘\CDR_Files");
		}

		private void OnLoad(object sender, EventArgs e)
		{
            CustomPropSectionParser cp = (CustomPropSectionParser)m_Config.GetSection("customSettings/SyncPairs");
			foreach (SyncPair p in cp.Pairs)
			{
			}
            //BrowserButtons
            m_btnBackward.Bounds = new Rectangle(0, 0, 40, 40);
            m_btnForward.Bounds = new Rectangle(42, 5, 30, 30);
            this.Controls.Add(m_btnForward);
            this.Controls.Add(m_btnBackward);
            m_btnBackward.ArrowAngle = 180;

            //ToolBar
            m_ToolBar.Font = new Font("微软雅黑", 10);
            //m_ToolBar.InsertButton("组织", "TB_MENU_SETUP", 70, TB_Menu_Setup);

            //TreeView
            Rectangle rcWnd = this.ClientRectangle;
            rcWnd.X = 1;
            rcWnd.Y = 85;
            rcWnd.Width = 200;
            rcWnd.Height -= 88;
            m_TreeView.Bounds = rcWnd;
            m_TreeView.BorderStyle = BorderStyle.None;
            Controls.Add(m_TreeView);
            //设置属性
            m_TreeView.HideSelection = false;
            m_TreeView.Tag = new object[1] { null };	//{TreeNode ActiveNode}
            //添加系统文件夹图标
            Shell32.SHFILEINFO sfi = new Shell32.SHFILEINFO();
            Shell32.SHGetFileInfo("SomeShit", Kernel32.File_Attribute.FILE_ATTRIBUTE_DIRECTORY,
                                ref sfi, Marshal.SizeOf(sfi),
                                Shell32.SHGFI.SHGFI_ICON | Shell32.SHGFI.SHGFI_USEFILEATTRIBUTES);
            m_TreeView.ImageList = new System.Windows.Forms.ImageList();
            m_TreeView.ImageList.ColorDepth = ColorDepth.Depth32Bit;
            m_TreeView.ImageList.Images.Add(Icon.FromHandle(sfi.hIcon));
            User32.DestroyIcon(sfi.hIcon);
            sfi.hIcon = IntPtr.Zero;

            //ListView
            //调整位置
            rcWnd = this.ClientRectangle;
            rcWnd.X = 205;
            rcWnd.Y = 85;
            rcWnd.Width -= 205;
            rcWnd.Height -= 88;
            m_ListView.Bounds = rcWnd;
            //设置属性
            m_ListView.BorderStyle = BorderStyle.None;
            m_ListView.HideSelection = false;
            m_ListView.View = View.LargeIcon;
            m_ListView.Tag = new object[1];		//{ListViewItem ActiveItem}
		}

		void ContextMenuStrip_Opening(object sender, CancelEventArgs e)
		{
		    e.Cancel = true;
		}

		private void OnFormClosing(object sender, FormClosingEventArgs e)
		{
			if (e.CloseReason.ToString() == "UserClosing")
			{
				if (m_FormCloseFlag == 0)
				{
					this.Hide();
					e.Cancel = true;
				}
				else if (m_FormCloseFlag == 1)
				{
					e.Cancel = false;
				}
			}
		}

		private void OnFormClosed(object sender, FormClosedEventArgs e)
		{
			this.Hide();
			File_Sync_Core.FS_DllFree();
		}

		private void OnNotifyIconMenuItemClick(object sender, EventArgs e)
		{
			ToolStripMenuItem HotItem = (ToolStripMenuItem)sender;
			switch (HotItem.Name)
			{
				case "MNUITEM_NI_FILESYNC_EXIT":
					{
						m_FormCloseFlag = 1;
						this.Close();
					}
					break;
			}
		}

        private void OnReSize(object sender, EventArgs e)
        {
            //TreeView
            Rectangle rcWnd = this.ClientRectangle;
            rcWnd.X = 1;
            rcWnd.Y = 85;
            rcWnd.Width = 200;
            rcWnd.Height -= 88;
            m_TreeView.Bounds = rcWnd;

            //ListView
            rcWnd = this.ClientRectangle;
            rcWnd.X = 205;
            rcWnd.Y = 85;
            rcWnd.Width -= 205;
            rcWnd.Height -= 88;
            m_ListView.Bounds = rcWnd;
        }
	}

	public static class File_Sync_Core
	{
		[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
		public class _JobItemInfo
		{
			public _JobItemInfo(File_SyncUI.Properties.SyncPair p)
			{
				lpExcludePath = p.ExcludePath;
				lpExcludeType = p.ExcludeType;
				lpSourceDir = p.SourceDir;
				lpTargetDir = p.TargetDir;
				bCheckMD5 = p.CheckMD5;
				bSafeCopy = p.SafeCopy;
			}
			[MarshalAs(UnmanagedType.LPWStr)]
			public string lpExcludePath;
			[MarshalAs(UnmanagedType.LPWStr)]
			public string lpExcludeType;
			[MarshalAs(UnmanagedType.LPWStr)]
			public string lpSourceDir;
			[MarshalAs(UnmanagedType.LPWStr)]
			public string lpTargetDir;
			public int bCheckMD5;
			public int bSafeCopy;
		}

		[DllImport("File_Sync_Core.dll", CharSet = CharSet.Unicode, SetLastError = true)]
		public static extern uint FS_DllInitialize();

		[DllImport("File_Sync_Core.dll", CharSet = CharSet.Unicode, SetLastError = true)]
		public static extern uint FS_DllFree();

		[DllImport("File_Sync_Core.dll", EntryPoint = "FS_AddJobItem", CharSet = CharSet.Unicode, SetLastError = true)]
		private static extern IntPtr _FS_AddJobItem(IntPtr pJobItem);

		public static IntPtr FS_AddJobItem(ref _JobItemInfo pJobItem)
		{
			IntPtr ptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(pJobItem));
			Marshal.StructureToPtr(pJobItem, ptr, false);
			IntPtr RetVal = _FS_AddJobItem(ptr);
			Marshal.PtrToStructure(ptr, pJobItem);
			Marshal.FreeCoTaskMem(ptr);
			return RetVal;
		}

		[DllImport("File_Sync_Core.dll", CharSet = CharSet.Unicode, SetLastError = true)]
		public static extern IntPtr FS_DelJobItem(IntPtr hJob);

		[DllImport("File_Sync_Core.dll", CharSet = CharSet.Unicode, SetLastError = true)]
		public static extern uint FS_StartMonitoring(IntPtr hJob);

		[DllImport("File_Sync_Core.dll", CharSet = CharSet.Unicode, SetLastError = true)]
		public static extern uint FS_StopMonitoring(IntPtr hJob);

		[DllImport("File_Sync_Core.dll", CharSet = CharSet.Unicode, SetLastError = true)]
		public static extern uint FS_ScanJobItem(IntPtr hJob);

		[DllImport("File_Sync_Core.dll", CharSet = CharSet.Unicode, SetLastError = true)]
		public static extern uint FS_OnSystemShuttingDown(IntPtr hWndParent);
	}
}
